Cosine

Implements Soft Cosine Similarity between strings.

The strings are first transformed in vectors of occurrences of k-shingles (sequences of k characters). In this n-dimensional space, the similarity between the two strings is the Cosine of their respective vectors.

The Cosine similarity between strings \(X\) and \(Y\) is the Cosine of the angle between the two strings as vectors. It is computed as: \(\frac{\vec{v_1} \cdot \vec{v_2}}{\lVert\vec{v_1}\rVert \times \lVert\vec{v_2}\rVert}\). Where, \(\vec{v_1}\) and \(\vec{v_2}\) are the vector representation of string \(X\) and \(Y\), respectively.

The distance is computed as \(1 - similarity(\vec{v_1}, \vec{v_2})\).

Author

Thibault Debatty, solonovamax

See also

Throws

if \(k \leqslant 0\)

Constructors

Link copied to clipboard
constructor(k: Int = DEFAULT_K)

Properties

Link copied to clipboard
val k: Int

Functions

Link copied to clipboard
open override fun distance(s1: String, s2: String): Double

Computes the Cosine distance of two strings.

fun distance(profile1: Map<String, Int>, profile2: Map<String, Int>): Double

Computes the Cosine distance of precomputed profiles.

Link copied to clipboard
fun profile(string: String): Map<String, Int>

Compute and return the profile of s, as defined by Ukkonen (Ukkonen 1992). The profile is the number of occurrences of k-shingles, and is used to compute q-gram similarity, Jaccard index, etc. Pay attention: the memory requirement of the profile can be up to \(k \times \text{size of the string}\)

Link copied to clipboard
open override fun similarity(s1: String, s2: String): Double

Computes the Cosine similarity of two strings.

fun similarity(profile1: Map<String, Int>, profile2: Map<String, Int>): Double

Computes the Cosine similarity of precomputed profiles.